/*
most useful intersection tests, in motion and static
*/
#ifndef _PHYSICS_H
#define _PHYSICS_H

class Rect;
struct Polygon2;
struct Point2;
class Circle;

//? add comments
class Physics{

public:
	static Physics* Instance();

	bool collide_Rect_Rect_S(Rect* r1, Rect* r2);
	bool rectToRect(Rect* s, Rect* d, Point2* v, Point2* n, float* t_min);
	bool circleVToRect(float radius, Point2* position, Rect* rect, Point2* V, Point2* n, float* t_min);
	bool rayToHorLine(float lineY, float lineXlo, float lineXhi, Point2* rayO, Point2* rayD, float* t);
	bool rayToVertLine(float lineX, float lineYlo, float lineYhi, Point2* rayO, Point2* rayD, float* t);
	bool rayToRect(Rect* rect, Point2* o, Point2* d, float* t_min, Point2* normal);
	bool rayToSegment(Point2* s1, Point2* s2, Point2* rayO, Point2* rayD, Point2* n, float* t);
	bool rayToCircle(Point2* origin, float radius, Point2* o, Point2* V, float* t_min);
	bool pointInRect(float x, float y, Rect* rect);
	bool rectToSegment(Rect* rect, Point2* s1, Point2* s2, Point2* v, Point2* n, float* t);
	bool intersectSegments(const Point2* p1, const Point2* p2, const Point2* p3, const Point2* p4, Point2* ret);

protected:
   Physics();
private:
	static Physics* _instance;

	enum{DONT_INTERSECT=0, DO_INTERSECT=1, PARALLEL=2};

	int linesIntersect(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float* x, float* y);
	int linesIntersectFast(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float* x, float* y);
};
extern float TTT;
#endif
